/**
 * @file json_parser.h
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef ACL_JSON_PARSER_H
#define ACL_JSON_PARSER_H

#include "nlohmann/json.hpp"
#include "acl/acl_base.h"
#include "log_inner.h"
#include "utils/cann_info_utils.h"

namespace acl {
    class JsonParser {
    public:
        static aclError ParseJsonFromFile(const char_t *const fileName, nlohmann::json &js);
        static aclError GetJsonCtxByKey(
            const char_t *const fileName, std::string &strJsonCtx, const std::string &subStrKey, bool &found);
        static aclError GetAttrConfigFromFile(
            const char_t *const fileName, std::map<aclCannAttr, CannInfo> &cannInfoMap);
        static aclError GetDefaultDeviceIdFromFile(const char_t *const fileName, int32_t& devId);

    private:
        static void GetMaxNestedLayers(const char_t *const fileName, const size_t length,
                                       size_t &maxObjDepth, size_t &maxArrayDepth);
        static bool IsValidFileName(const char_t *const fileName);

        static bool ParseJson(const char_t *const fileName, nlohmann::json &js, const size_t fileLength);

        static const nlohmann::json& GetCfgJsonByKey(const nlohmann::json &js, const std::string &key) {
            return js.at(key);
        }

        static std::string GetCfgStrByKey(const nlohmann::json &js , const std::string &key) {
            return js.at(key).get<std::string>();
        }
        
        static bool ContainKey(const nlohmann::json &js, const std::string &key) {
            return (js.find(key) != js.end());
        }
    };
}
#endif // ACL_JSON_PARSER_H
